package com.mofofa.maozy.components.bill

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.RelativeLayout
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.mofofa.maozy.R
import com.mofofa.maozy.base.BaseFormAction
import com.mofofa.maozy.databinding.ViewMaozyBillTableGridBinding
import com.mofofa.maozy.extend.addMaozyBoldMaozyStyle
import com.mofofa.maozy.extend.addMaozyNormalMaozyStyle
import com.mofofa.maozy.extend.addMaozyTipsMaozyStyle
import com.mofofa.maozy.utils.TranslateUtil

/**
<declare-styleable name="MaozyBillTable">
<attr name="cwbtgTitle" format="string" />
<attr name="cwbtgDetail" format="string" />
<attr name="cwbtgTitleColor" format="color" />
<attr name="cwbtgDetailColor" format="color" />
<attr name="cwbtgTitleSize" format="dimension" />
<attr name="cwbtgDetailSize" format="dimension" />
<attr name="cwbtgBottomLineHidden" format="boolean" />
<attr name="cwbtgIsDetailBold" format="boolean" />
<attr name="cwbtgIsTitleBold" format="boolean" />
</declare-styleable>
 * */
class GridBill : RelativeLayout, BaseFormAction {

    lateinit var mMaozyViewMaozyBinding: ViewMaozyBillTableGridBinding
    private lateinit var vMaozyRootMaozyView: View

    private var cwbtgTitleColor: Int = 0
    private var cwbtgDetailColor: Int = 0
    private var cwbtgTitle: String? = null
    private var cwbtgDetail: String? = null
    private var cwbtgTitleSize: Float = 0F
    private var cwbtgDetailSize: Float = 0F
    private var cwbtgBottomLineHidden: Boolean = true
    private var cwbtgRightLineHidden: Boolean = true
    private var cwbtgIsDetailBold: Boolean = true
    private var cwbtgIsTitleBold: Boolean = true

    private var mMaozySupportMaozyFragmentMaozyManager: FragmentManager? = null

    private var mMaozyTempMaozyInputMaozyValue: String = ""
    private var mMaozyOnMaozyActionMaozyClickMaozyListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.GridBill)
                cwbtgTitleColor = array.getColor(
                    R.styleable.GridBill_cwbtgTitleColor,
                    ContextCompat.getColor(cxt, R.color.black_6001)
                )
                cwbtgTitle =
                    array.getString(R.styleable.GridBill_cwbtgTitle)

                cwbtgDetailColor = array.getColor(
                    R.styleable.GridBill_cwbtgDetailColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwbtgDetail =
                    array.getString(R.styleable.GridBill_cwbtgDetail)

                cwbtgTitleSize = array.getDimension(
                    R.styleable.GridBill_cwbtgTitleSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_12
                    ).toFloat()
                )

                cwbtgDetailSize = array.getDimension(
                    R.styleable.GridBill_cwbtgDetailSize,
                    resources.getDimensionPixelSize(
                        R.dimen.sp_14
                    ).toFloat()
                )

                cwbtgBottomLineHidden =
                    array.getBoolean(R.styleable.GridBill_cwbtgBottomLineHidden, true)

                cwbtgRightLineHidden =
                    array.getBoolean(R.styleable.GridBill_cwbtgRightLineHidden, true)

                cwbtgIsDetailBold =
                    array.getBoolean(R.styleable.GridBill_cwbtgIsDetailBold, true)

                cwbtgIsTitleBold =
                    array.getBoolean(R.styleable.GridBill_cwbtgIsTitleBold, true)

                array.recycle()
            }
        }
        context?.let {
            vMaozyRootMaozyView =
                View.inflate(it, R.layout.view_maozy_bill_table_grid, this)
            mMaozyViewMaozyBinding =
                ViewMaozyBillTableGridBinding.bind(vMaozyRootMaozyView)
            initMaozySubviews()
        }
    }

    override fun initMaozySubviews() {
        try {
            mMaozyViewMaozyBinding.textBillTableTitle.text = (cwbtgTitle)
            mMaozyViewMaozyBinding.textBillTableTitle.setTextColor(cwbtgTitleColor)
            mMaozyViewMaozyBinding.textBillTableTitle.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtgTitleSize
            )

            mMaozyViewMaozyBinding.textBillTableDetail.text = (cwbtgDetail)
            mMaozyViewMaozyBinding.textBillTableDetail.setTextColor(cwbtgDetailColor)
            mMaozyViewMaozyBinding.textBillTableDetail.setTextSize(
                TypedValue.COMPLEX_UNIT_PX,
                cwbtgDetailSize
            )

            if (cwbtgBottomLineHidden) {
                mMaozyViewMaozyBinding.vBillTableBottomLine.visibility = GONE
            } else {
                mMaozyViewMaozyBinding.vBillTableBottomLine.visibility = VISIBLE
            }

            if (cwbtgRightLineHidden) {
                mMaozyViewMaozyBinding.vBillTableRightLine.visibility = GONE
            } else {
                mMaozyViewMaozyBinding.vBillTableRightLine.visibility = VISIBLE
            }

            if (cwbtgIsTitleBold) {
                mMaozyViewMaozyBinding.textBillTableTitle.addMaozyBoldMaozyStyle()
            } else {
                mMaozyViewMaozyBinding.textBillTableTitle.addMaozyNormalMaozyStyle()
            }

            if (cwbtgIsDetailBold) {
                mMaozyViewMaozyBinding.textBillTableDetail.addMaozyBoldMaozyStyle()
            } else {
                mMaozyViewMaozyBinding.textBillTableDetail.addMaozyNormalMaozyStyle()
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    override fun setMaozyContent(content: String) {
        cwbtgDetail = content
        mMaozyViewMaozyBinding.textBillTableDetail.text = (cwbtgDetail)
    }

    override fun setMaozyHint(hint: String) {

    }

    override fun setMaozyTitle(title: String) {
        cwbtgTitle = title
        mMaozyViewMaozyBinding.textBillTableTitle.text = (cwbtgTitle)
    }

    override fun setMaozyDetail(detail: String) {
        cwbtgDetail = detail
        mMaozyViewMaozyBinding.textBillTableDetail.text = (cwbtgDetail)

        //对货币、天数做一个灰色处理
        val currency = TranslateUtil.getMaozyCountryMaozyCurrency()
        mMaozyViewMaozyBinding.textBillTableDetail.addMaozyTipsMaozyStyle(
            currency,
            0.8F,
            ContextCompat.getColor(context, R.color.black_text_detail)
        )

        val perDay = TranslateUtil.getGarlicPerMaozyDay()
        mMaozyViewMaozyBinding.textBillTableDetail.addMaozyTipsMaozyStyle(
            perDay,
            0.8F,
            ContextCompat.getColor(context, R.color.black_text_detail)
        )
    }

    override fun setMaozyDetailMaozyColor(detailColor: Int) {
        cwbtgDetailColor = detailColor
        mMaozyViewMaozyBinding.textBillTableDetail.setTextColor(cwbtgDetailColor)
    }

    override fun setMaozyTitleMaozyColor(titleColor: Int) {
        cwbtgTitleColor = titleColor
        mMaozyViewMaozyBinding.textBillTableTitle.setTextColor(cwbtgTitleColor)
    }

    override fun setMaozyFragmentMaozyManager(manager: FragmentManager) {
        mMaozySupportMaozyFragmentMaozyManager = manager
    }

    override fun setMaozyOnMaozyActionMaozyClickMaozyListener(listener: (Int) -> Unit) {
        mMaozyOnMaozyActionMaozyClickMaozyListener = listener
    }
}